//
//  BlackTableViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/13.
//

#import "BlackTableViewCell.h"

@interface BlackTableViewCell ()

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation BlackTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.avatarImageV];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.cancelBtn];
    
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
        make.leading.mas_equalTo(16);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.avatarImageV.mas_trailing).offset(8);
        make.centerY.mas_equalTo(0);
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(70, 24));
    }];
}

- (void)setModel:(UserModel *)model {
    _model = model;
    [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text = model.nickName;
}

- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.clipsToBounds = YES;
        _avatarImageV.layer.cornerRadius = 25;
    }
    return _avatarImageV;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = kRGB(0x333333);
        _nameLabel.font = kFontMediium(16);
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}

- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.backgroundColor = kRGB(0xF2F2F2);
        _cancelBtn.layer.cornerRadius = 12;
        _cancelBtn.layer.masksToBounds = YES;
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = kFontMediium(12);
        @weakify(self)
        [[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.cancelBlock) {
                self.cancelBlock(self.model.id);
            }
        }];
    }
    return _cancelBtn;
}

@end
